<!DOCTYPE html>
<html lang="cmn-Hans" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>文档详情页</title>
    <link rel="stylesheet" th:href="@{/frame/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/frame/flyres/css/global.css}">
</head>
<body>
<div th:replace="customer/top :: topBar"></div>
<div class="layui-row">
    <div class="layui-card">
        <div class="layui-card-body" style="text-align: center">
            <div class="layui-input-inline" style="width: 50%">
                <input placeholder="请输入搜索关键词" class="layui-input" id="ikey">
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn" onclick="dodosearch()"><i class="layui-icon layui-icon-search"></i>搜索文档
                </button>
            </div>
        </div>
    </div>
</div>

<div class="layui-container">
    <div class="layui-row layui-col-space15" style="margin-top: 15px">
        <div class="layui-col-md9">
            <div class="layui-card">
                <div class="layui-card-header">
                    [[${d_document.title}]]
                    <a style="display:inline;position: absolute;right: 3%;cursor: pointer" href="javascript:"
                       onclick="showDetail(1)" id="expand"><i class="layui-icon layui-icon-screen-full"></i></a>
                </div>
                <div class="layui-card-body" style="height: 1440px;">
                    <object th:data="${'/document/getSVG?stname='+d_document.storedname+'&pageId=1'}"
                            width="100%" height="100%" id="pageData"></object>
                </div>
            </div>
            <div id="docPages"></div>
        </div>
        <div class="layui-col-md3">
            <div class="layui-row layui-col-space15">
                <div class="layui-card">
                    <div class="layui-card-header">
                        上传者
                    </div>
                    <div class="layui-card-body">
                        <a class="fly-nav-avatar"
                           th:href="@{'/customer/home?uid='+${d_document.uploaduser.phone * 6}}">
                            <img th:src="@{'/picture/getAvatar?fileName='+${d_document.uploaduser.avatar}}"/>
                            <div th:text="${d_document.uploaduser.username}"></div>
                        </a>
                    </div>
                </div>
                <div class="layui-card">
                    <div class="layui-card-body">
                        <div class="layui-row layui-col-space15">
                            <div class="layui-col-md6" style="text-align: center">

                                <a class="layui-btn layui-btn-normal"
                                   href="javascript:layui.layer.msg('您必须先登录才能下载本站的文件！');"
                                   th:if="${session.current_customer} eq null">
                                    <i class="layui-icon layui-icon-download-circle"></i>
                                    <span th:text="'下载&nbsp;&nbsp;'+${d_document.neededintegral}+'积分'"></span>
                                </a>
                                <a class="layui-btn layui-btn-normal" id="dla" href="javascript:" th:onclick="'dl(this,'+${d_document.docid}+')'"
                                   onclick="dl(this,${d_document.docid})" th:if="${session.current_customer} ne null">
                                    <i class="layui-icon layui-icon-download-circle"></i>
                                    <span th:text="'下载&nbsp;&nbsp;'+${d_document.neededintegral}+'积分'"></span>
                                </a>
                            </div>
                            <div class="layui-col-md6" style="text-align: center">
                                <button class="layui-btn layui-btn-normal" onclick="docollect()"
                                        th:if="${session.current_customer} eq null">
                                    <i class="layui-icon layui-icon-rate"></i>收藏文档
                                </button>
                                <script>
                                    function docollect() {
                                        layui.layer.msg('您尚未登录，请登录后重试。')
                                    }
                                </script>
                                <button class="layui-btn layui-btn-normal" onclick="doremove(this)"
                                        th:if="${session.current_customer ne null && iscollected}">
                                    <i class="layui-icon layui-icon-rate-solid"></i>取消收藏
                                </button>
                                <button class="layui-btn layui-btn-normal" onclick="doadd(this)"
                                        th:if="${session.current_customer ne null && !iscollected}">
                                    <i class="layui-icon layui-icon-rate"></i>收藏文档
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <script>
                    function doadd(obj) {
                        const xhr = new XMLHttpRequest();
                        xhr.open('post', '[[@{/collect/addCollect}]]', false);
                        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                        xhr.send('docid=' + [[${d_document.docid}]]);
                        // alert(JSON.parse(xhr.responseText).msg);
                        obj.innerHTML = '<i class="layui-icon layui-icon-rate-solid"></i>取消收藏';
                        obj.setAttribute('onclick', 'doremove(this)')
                    }

                    function doremove(obj) {
                        const xhr = new XMLHttpRequest();
                        xhr.open('post', '[[@{/collect/removeCollect}]]', false);
                        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                        xhr.send('docid=' + [[${d_document.docid}]]);
                        // alert(JSON.parse(xhr.responseText).msg);
                        obj.innerHTML = '<i class="layui-icon layui-icon-rate"></i>收藏文档';
                        obj.setAttribute('onclick', 'doadd(this)')
                    }
                </script>

                <div class="layui-card">
                    <div class="layui-card-header">说点什么吧</div>
                    <div class="layui-card-body">
                        <textarea id="comment" style="display: none;"></textarea>
                        <button class="layui-btn" onclick="commitcomment(0)"
                                th:if="${session.current_customer} eq null">发表评论
                        </button>
                        <button class="layui-btn" onclick="commitcomment(1)"
                                th:if="${session.current_customer} ne null">发表评论
                        </button>
                    </div>
                </div>
                <div class="layui-card" th:each="it : ${d_document.commentList}">
                    <div class="layui-card-body">
                        <a class="fly-nav-avatar" th:href="@{'/customer/home?uid='+${it.customer.phone * 6}}">
                            <img th:src="@{'/picture/getAvatar?fileName='+${it.customer.avatar}}"/>
                            [[${it.customer.username}]]
                        </a>
                        <br>
                        发表于：[[${it.publishtime}]]
                        <hr>
                        <div th:utext="${it.content}"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <a id="hiddena" style="display: none;">隐藏下载标签</a>
    <input type="hidden" th:if="${session.current_customer} eq null" th:value="0" id="userPhone">
    <input type="hidden" th:if="${session.current_customer} ne null" th:value="${session.current_customer.phone}" id="userPhone">
</div>
<script type="text/javascript" th:src="@{/frame/layui/layui.js}"></script>
<script>
    layui.use(['element', 'layedit', 'laypage'], function () {
        const laypage = layui.laypage;
        const layedit = layui.layedit;
        const $ = layui.$;
        laypage.render({
            elem: 'docPages',
            count: [[${d_document.pages}]],
            limit: 1,
            layout: ['prev', 'page', 'next', 'skip'],
            jump: function (obj, first) {
                if (!first) {
                    $('#pageData').attr('data', '[[@{/document/getSVG}]]'+'?stname='+'[[${d_document.storedname}]]'+'&pageId=' + obj.curr);
                    $('#expand').attr('onclick', 'showDetail(' + obj.curr + ')');
                }
            }
        });

        const commentarea = layedit.build('comment', {
            tool: [
                'strong' //加粗
                , 'italic' //斜体
                , 'underline' //下划线
                , 'del' //删除线
                // ,'|' //分割线
                , 'left' //左对齐
                , 'center' //居中对齐
                , 'right' //右对齐
                , 'link' //超链接
                , 'unlink' //清除链接
                , 'face' //表情
                //,'image' //插入图片
                // ,'help' //帮助
            ]
        });

        window.commitcomment = function (status) {
            if (status === 0) {
                layer.msg('您尚未登录，暂时无法评论！', {icon: 0, time: 2000});
            } else {
                $.post('[[@{/comment/commitComment}]]', {
                    'document.docid':[[${d_document.docid}]],
                    'customer.phone':$('#userPhone').val(),
                    content: layedit.getContent(commentarea)
                }, function (res) {
                    layer.msg(res.msg);
                });
            }
        };


        window.dl = function (obj, docid) {
            $.ajax({
                type: 'post',
                async: false,
                url: '[[@{/document/dlJudge}]]',
                dataType: 'json',
                data: {
                    docid: docid
                },
                success: function (res) {
                    if (res.issuccess) {
                        layer.msg(res.msg, {icon: 1});

                        //此处下载链接暴露
                        //防止暴力破解下载，添加校验码dlCheckCode

                        const hiddena = $('#hiddena');
                        hiddena.attr('href', '[[@{/document/downloadDoc?docid=}]]' + docid + '&dlCheckCode=' + res.count);
                        hiddena.attr('onclick', '');
                        hiddena.attr('download', '[[${d_document.title}]]'+'.'+'[[${d_document.suffixicon.suffix}]]');
                        document.getElementById('hiddena').click();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error: function () {
                    layer.msg('网络错误！', {icon: 2});
                }
            });
        };

        window.dodosearch = function () {
            location.href = '[[@{/document/searchDoc?keyword=}]]' + $('#ikey').val();
        };

        window.showDetail = function (pageid) {
            layer.open({
                maxmin: true,
                anim: 1,
                area: ['80%', '80%'],
                type: 2,
                content: "[[@{'/document/getSVG?stname='+${d_document.storedname}+'&pageId='}]]" + pageid
            });
        }
    });
</script>
</body>
</html>